﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TaskListManager
{
   [Serializable()]
   class TaskList
   {
      private List<TaskEntry> m_sEntries;

      public TaskList()
      {
         m_sEntries = new List<TaskEntry>();
      }

      protected int GetNextAvailableTaskId()
      {
         int lHighest = 0;
         for (int i = 0; i < m_sEntries.Count; i++)
         {
            if (m_sEntries[i].TaskId > lHighest)
            {
               lHighest = m_sEntries[i].TaskId;
            }
         }
         return (lHighest + 1);
      }

      public int GetTaskCount()
      {
         return m_sEntries.Count();
      }

      public int GetTaskIdAtIndex(int lIdx)
      {
         int lEntryId = -1;
         if ((lIdx > -1) && (lIdx < m_sEntries.Count))
         {
            lEntryId = m_sEntries[lIdx].TaskId;
         }
         return lEntryId;
      }

      public TaskEntry GetTaskAtIndex(int lIdx)
      {
         TaskEntry sEntry = new TaskEntry();
         if ((lIdx > -1) && (lIdx < m_sEntries.Count))
         {
            sEntry = m_sEntries[lIdx];
         }
         return sEntry;
      }

      public TaskEntry GetTaskWithIdent(int lIdent)
      {
         TaskEntry sEntry = new TaskEntry();
         int lIdx = GetTaskIndexForId(lIdent);
         if (lIdx != -1)
         {
            sEntry = m_sEntries[lIdx];
         }
         return sEntry;
      }

      public int GetTaskIndexForId(int lIdent)
      {
         int lIdx = -1;
         for (int i = 0; i < m_sEntries.Count; i++)
         {
            if (m_sEntries[i].TaskId == lIdent)
            {
               lIdx = i;
               break;
            }
         }
         return lIdx;
      }

      public int AddTask(string strDesc, long lStartDate, long lEndDate, short nPercentComplete, bool bCompleted, int lOwner)
      {
         TaskEntry t = new TaskEntry();
         t.OwnerUser = lOwner;
         t.Complete = bCompleted;
         t.Description = strDesc;
         t.EndDate = lEndDate;
         t.StartDate = lStartDate;
         t.TaskId = GetNextAvailableTaskId();
         m_sEntries.Add(t);
         return t.TaskId;
      }

      protected int TaskIndex(int lTaskId)
      {
         int lIdx = -1;
         for (int i = 0; i < m_sEntries.Count; i++)
         {
            if (m_sEntries[i].TaskId == lTaskId)
            {
               lIdx = i;
               break;
            }
         }
         return lIdx;
      }

      public void ModifyTaskDesc(int lTaskId, string strDesc)
      {
         int lIdx = TaskIndex(lTaskId);
         if (lIdx != -1)
         {
            m_sEntries[lIdx].Description = strDesc;
         }
      }

      public void ModifyTaskStartDate(int lTaskId, long lStart)
      {
         int lIdx = TaskIndex(lTaskId);
         if (lIdx != -1)
         {
            m_sEntries[lIdx].StartDate = lStart;
         }
      }

      public void ModifyTaskEndDate(int lTaskId, long lEnd)
      {
         int lIdx = TaskIndex(lTaskId);
         if (lIdx != -1)
         {
            m_sEntries[lIdx].EndDate = lEnd;
         }
      }

      public void ModifyTaskPercentComplete(int lTaskId, short nComplete)
      {
         int lIdx = TaskIndex(lTaskId);
         if (lIdx != -1)
         {
            m_sEntries[lIdx].PercentComplete = nComplete;
         }
      }

      public void ModifyTaskCompleted(int lTaskId, bool bCompleted)
      {
         int lIdx = TaskIndex(lTaskId);
         if (lIdx != -1)
         {
            m_sEntries[lIdx].Complete = bCompleted;
         }
      }

      public void ModifyTaskOwner(int lTaskId, int lOwnerId)
      {
         int lIdx = TaskIndex(lTaskId);
         if (lIdx != -1)
         {

            m_sEntries[lIdx].OwnerUser = lOwnerId;
         }
      }

      public void RemoveTask(int lTaskId)
      {
         for (int i = 0; i < m_sEntries.Count; i++)
         {
            if (m_sEntries[i].TaskId == lTaskId)
            {
               m_sEntries.Remove(m_sEntries[i]);
               break;
            }
         }
      }
   }
}
